
package es.ucm.disia.rcei.example.voice2;

import edu.cmu.sphinx.frontend.util.Microphone;
import edu.cmu.sphinx.recognizer.Recognizer;
import edu.cmu.sphinx.recognizer.RecognizerState;
import edu.cmu.sphinx.result.Result;
import edu.cmu.sphinx.util.props.ConfigurationManager;
import edu.cmu.sphinx.util.props.PropertyException;

import java.io.IOException;
import java.net.URL;

/**
 * A simple speech application built using Sphinx-4. 
 * This application uses the Sphinx-4 endpointer, which automatically segments incoming audio into utterances and silences.*/
public class VoiceRecognizer implements Runnable
{
	private String orderRecognized;
	private ConfigurationManager cm;
	private Recognizer recognizer;
	private Microphone microphone;
	long tiempo1;
	long tiempo2;
	
    public VoiceRecognizer() 
    {
    	this.initParams("rceiVoice.config.xml");
    	this.orderRecognized="";
    }
	
    public VoiceRecognizer(String voiceFileConfig) 
    {
    	this.initParams(voiceFileConfig);
    	this.orderRecognized="";
    }
    
	public void initConfigManager(String voiceFileConfig) 
	{		
		try 
		{
			URL url = this.getClass().getResource(voiceFileConfig); //"rceiVoice.config.xml");
			this.cm = new ConfigurationManager(url);
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		} 
		catch (PropertyException e) 
		{
			e.printStackTrace();
		}
	}
	
	public void initParams(String voiceFileConfig) 
	{		
		initConfigManager(voiceFileConfig);
		try 
		{
			this.recognizer = (Recognizer) cm.lookup("recognizer");
			this.microphone = (Microphone) cm.lookup("microphone");
		} 		
		catch (PropertyException e) 
		{
			e.printStackTrace();
		} 
		catch (InstantiationException e) 
		{
			e.printStackTrace();
		}
	}
	
	public String getOrderRecognized()
	{
		return orderRecognized;
	}

	/*public String recognizeSubject()
	{
		//return recognize("rceiVoice.config.xml",
						 "RCEI NPCS: Bhiksha | Evandro | Paul | Philip | Rita | Will");
	}
	
	public String recognizeProcess()
	{
		return recognize("rceiVoice2.config.xml","RCEI orders: hello | go");
	}*/
	
	 /**
     * Turns on the microphone and starts recognition
     */
    public boolean  microphoneOn() 
    {
        if (microphone.getAudioFormat() == null) 
        {
            return false;
        } 
        else 
        {
            new Thread(this).start();
            return true;
        }
    }

    /**
     * Turns off the microphone, ending the current recognition in progress
     */
    public void microphoneOff() 
    {
        microphone.stopRecording();
    }

    /**
     * Allocates resources necessary for recognition.
     */
    public void startup() throws IOException 
    {
    	recognizer.allocate();
    }

    /**
     * Releases recognition resources
     */
    public void shutdown() 
    {
        microphoneOff();

        if (recognizer.getState() == RecognizerState.ALLOCATED) 
        {
            recognizer.deallocate();
        }
    }

    /**
     * Performs a single recognition
     */
    public void run() 
    {    	
    	microphone.clear();
        microphone.startRecording();
        //while (true)
		//{
        	System.out.println("run() Start speaking...\n");
        	
        	Result result = recognizer.recognize();
        	microphone.stopRecording();
        	
	        if (result != null) 
	        {
	        	this.orderRecognized = result.getBestFinalResultNoFiller();
	        	
	            if (orderRecognized.length() > 0) 
	            {           
	            	System.out.println("You said: " + orderRecognized + ".");  	            	
	            }
	            else
	            {             
	            	System.out.println("I can't hear what you said.\n");
	            	//microphone.clear();
	                //microphone.startRecording();
	            }
	        }
		//}
        //microphone.stopRecording();
    }
    
}
